﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	CamlQueryBuilderExts.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Diagnostics;
using Microsoft.SharePoint;
using Smart.Common.Entities;

namespace Smart.Common
{
	public static class CamlQueryBuilderExts
	{
		[DebuggerStepThrough]
		public static IQueryBuilder<ICamlQueryExecutor> Query(this SPList list, MetaFields meta)
		{
			var qb = new CamlQueryBuilder<ICamlQueryExecutor>(meta, new CamlQueryExecutor(list));

			qb.Executor.Query = qb;

			return qb;
		}

		[DebuggerStepThrough]
		public static IQueryBuilder<ICamlQueryExecutor> Query(this SPList list)
		{
			return Query(list, MetaCtx.MetaList(list.ParentWeb, list.ID));
		}

		[DebuggerStepThrough]
		public static IQueryBuilder<ICamlQueryExecutor> Query(this SPWeb web, MetaFields meta)
		{
			var qb = new CamlQueryBuilder<ICamlQueryExecutor>(meta, new CamlQueryExecutor(web));

			qb.Executor.Query = qb;

			return qb;
		}

		[DebuggerStepThrough]
		public static IQueryBuilder<ICamlQueryExecutor> Query(this SPWeb web, string listUrl)
		{
			var mw = MetaCtx.GetWeb(web);

			var ml = mw.FindList(listUrl);

			var qb = new CamlQueryBuilder<ICamlQueryExecutor>(ml, new CamlQueryExecutor(web));

			qb.Executor.Query = qb;

			qb.Executor.SiteQuery.FilterListsByUrl(false, listUrl);

			return qb;
		}
	}
}